package com.zpiz.controller;

import cn.hutool.http.HttpUtil;
import com.apiz.model.BaiduParams;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

@RestController
@RequestMapping("/happy")
public class HappyController {

    //随机获取图片
    @PostMapping("/sjbz/api.php")
    public String randImages(@RequestBody(required = false) String body, HttpServletRequest request) throws Exception {
        //public String getAvatarUrlByPost(AvatarParams avatarParams, HttpServletRequest request) throws Exception {
        //https://restapi.amap.com/v3/weather/weatherInfo?
        // https 不能是http 否则拿不到图片
        String avatarUrl = "https://www.loliapi.com/acg/pp/";
//        HashMap<String, Object> paramMap = new HashMap<>();
//        paramMap.put("type", "json");
//        paramMap.put("tx", "b1");
//        paramMap.put("key", "2yta7ZzxPfP6YqZZLzQi413D3B");
//        HttpRequest get = HttpUtil.createGet(avatarUrl, true);
        String redirectUrl = getRedirectUrl(avatarUrl);

        System.out.println("图片地址"+redirectUrl);
        return redirectUrl;
    }


    /**
     * 获取重定向地址
     * @param path
     * @return
     * @throws Exception
     */
    private String getRedirectUrl(String path) throws Exception {
        HttpURLConnection conn = (HttpURLConnection) new URL(path)
                .openConnection();
        conn.setInstanceFollowRedirects(false);
        conn.setConnectTimeout(5000);
        String location = conn.getHeaderField("Location");
        return location;
    }

    //随机获取毒鸡汤
    @PostMapping("/text")
    public String poisonChicken(@RequestBody(required = false) String body, HttpServletRequest request) throws Exception {
        //public String getChickenSoupByPost(ChickenSoupParams body, HttpServletRequest request) throws Exception {
        String chickenSoupUrl = "http://api.btstu.cn/yan/api.php";
        HashMap<String, Object> paramMap = new HashMap<>();

        String text = HttpUtil.get(chickenSoupUrl, paramMap);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{\"text\": \"");
        stringBuffer.append(text);
        stringBuffer.append("\"}");
        return stringBuffer.toString();
    }

    //百度热点数据
    @PostMapping("/baiduInfo")
    public String getBaiduInfoByPost(@RequestBody(required = false) BaiduParams baiduParams, HttpServletRequest request) {
        //public String getBaiduInfoByPost(BaiduParams baiduParams, HttpServletRequest request) {
        //https://restapi.amap.com/v3/weather/weatherInfo?
        String baiduUrl = "https://www.coderutil.com/api/resou/v1/baidu";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("access-key", "2a73055beafb826cf0aaf0d284d9eede");
        paramMap.put("secret-key", "3fe196bd0a439eef303155b3870b71d5");
        if (baiduParams == null) {
            baiduParams = new BaiduParams();
        }

        if (baiduParams.getSize() == null ||  "".equals(baiduParams.getSize())) {
            paramMap.put("size", 10);
        }else {
            paramMap.put("size", baiduParams.getSize());
        }
        String result = HttpUtil.get(baiduUrl, paramMap);
        return result;
    }
}
